package org.jeecg.modules.zhima.wx.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.jeecg.modules.zhima.base.util.HttpClientUtils;
import org.jeecg.modules.zhima.wx.domain.WxCode;
import org.jeecg.modules.zhima.wx.service.IWxMinipService;
import org.jeecg.modules.zhima.wx.template.Template;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zhigangding
 * @date 2020/2/25
 */
@Slf4j
@Service
public class WxMinipServiceImpl implements IWxMinipService {

    @Value("${wx.minip.appId}")
    private String appId;
    @Value("${wx.minip.secret}")
    private String secret;
    @Value("${wx.url.token}")
    private String tokenUrl;
    @Value("${wx.url.message.send}")
    private String messageSendUrl;
    @Value("${wx.url.msg_sec.check}")
    private String msgSecCheck;
    @Value("${wx.url.getwxacodeunlimit}")
    private String getwxacodeunlimit;

    private static final String ACCESS_TOKEN = "ACCESS_TOKEN";

    @Override
    public String getToken() {
        String token = tokenUrl.replace("APPID", appId);
        token = token.replace("SECRET", secret);
        String result = HttpClientUtils.sendGet(token);
        JSONObject object= JSON.parseObject(result);
        return object.getString("access_token");
    }

    @Override
    public String getwxacodeunlimit(WxCode wxCode) {
        try {
            String urlString = getwxacodeunlimit.replace(ACCESS_TOKEN, this.getToken());
            JSONObject param = new JSONObject();
            param.put("scene", wxCode.getScene());
            param.put("page", wxCode.getPage());
//            param.put("width", wxCode.getWidth());
//            param.put("auto_color", wxCode.getAutoColor());
//            param.put("line_color", wxCode.getLineColor());
//            param.put("is_hyaline", wxCode.getIsHyaline());
            log.info("调用生成微信URL接口传参:" + param);
            return HttpClientUtils.httpPostImage(urlString, param);
        } catch (Exception e) {
            log.error("调用小程序生成微信永久小程序码URL接口异常",e);
        }
        return null;
    }

    @Override
    public boolean send(Template template) {
        String requestUrl=messageSendUrl.replace(ACCESS_TOKEN, this.getToken());
        String outPutStr = JSON.toJSONString(template);
//        log.info("wx message send outPutStr:{}", outPutStr);
        JSONObject jsonResult=HttpClientUtils.httpsRequest(requestUrl, "POST", outPutStr);
        if(jsonResult!=null){
            int errorCode=jsonResult.getIntValue("errcode");
            return (errorCode==0);
        }
        return false;
    }

    @Override
    public boolean msgSecCheck(String content) {
        String requestUrl=msgSecCheck.replace(ACCESS_TOKEN, this.getToken());
        String msg = "{'content':'MESSAGE'}";
        msg = msg.replace("MESSAGE", content);
        Map<String, String> map = new HashMap<>();
        map.put("content", content);
        String res = HttpClientUtils.sendPost2(requestUrl, map);
        JSONObject jsonResult=JSONObject.parseObject(res);
        log.info("msg_sec_check res:{}", jsonResult.toString());
        if(jsonResult!=null){
            int errorCode=jsonResult.getIntValue("errcode");
            return (errorCode==0);
        }
        return false;
    }

    @Override
    public boolean imgSecCheck(String content) {
        return false;
    }


}
